﻿/////////////////////////////////////////////////
//文件名:Serialize
//描  述:
//创建者:刘凡平(Iveely Liu)
//邮  箱:945222889@qq.com
//组  织:Iveely
//年  份:2012/5/11 10:51:50
///////////////////////////////////////////////


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace IveelySE.BigData.Common
{
    /// <summary>
    /// 序列化
    /// </summary>
    public class Serialize
    {
        /// <summary>
        /// 将对象序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="fileName"> </param>
        public static void Serailzable(Object obj, string fileName)
        {
            //如果包含建立目录信息
            if (fileName.Contains("\\"))
            {
                //创建目录
                if (!Directory.Exists(fileName.Substring(0, fileName.LastIndexOf('\\'))))
                {
                    Directory.CreateDirectory(fileName.Substring(0, fileName.LastIndexOf('\\')));
                }
            }
            //如果文件存在
            //if (File.Exists(fileName))
            //{
                //则删除
                File.Delete(fileName);
            //}
            //文件流
            FileStream fs = null;
            try
            {
                //文件流创建
                fs = new FileStream(fileName, FileMode.Create);
                //二进制对象
                var bf = new BinaryFormatter();
                //执行序列化
                bf.Serialize(fs, obj);
            }
            catch (Exception exception)
            {
                Console.Write(exception.Message);
            }
            finally
            {
                //如果文件流依然打开
                if (fs != null)
                {
                    //则关闭
                    fs.Close();
                }
            }
        }
        /// <summary>
        /// 反序列化对象
        /// </summary>
        /// <param name="fileName">文件</param>
        /// <returns>返回对象</returns>
        public static Object UnSerailzable(string fileName)
        {
            try
            {
                if (File.Exists(fileName))
                {
                    //文件流
                    var fs = new FileStream(fileName, FileMode.Open);
                    //二进制对象
                    var bf = new BinaryFormatter();
                    //执行序列化
                    Object obj = bf.Deserialize(fs);
                    //关闭流，这个很重要
                    fs.Close();
                    //返回
                    return obj;
                }
            }
            catch(Exception ex)
            {
                Common.Log.Write(ex.Message);
            }
            return null;
        }

        /// <summary>
        /// 字节转换为对象
        /// </summary>
        /// <param name="bytes">字节数组</param>
        /// <returns>返回对象</returns>
        public static Object Byte2Obj(byte[] bytes)
        {
            MemoryStream rems = new MemoryStream(bytes);
            return new BinaryFormatter().Deserialize(rems);
        }

        /// 对象转换为字节数组
        /// </summary>
        /// <param name="obj">需要传输的对象</param>
        /// <returns>返回字节数组</returns>
        public static byte[] Obj2Byte(object obj)
        {
            MemoryStream memoryStream = new MemoryStream();
            new BinaryFormatter().Serialize(memoryStream, obj);
            return memoryStream.GetBuffer();
        }
    }
}
